﻿using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace WPay.Infrastructure.Unity
{
    public static class PredicateBuilder
    {
        public static Expression<Func<T, bool>> True<T>() { return f => true; }
        public static Expression<Func<T, bool>> False<T>() { return f => false; }
        public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
        {  
            var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);
            var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
            return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
        }

        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
        {
            return first.Compose(second, Expression.And);
        }

        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
        {
            return first.Compose(second, Expression.Or);
        } 
        
       
    }


    public static class PredicateBuilder_es
    {
        public static Expression<Func<QueryContainerDescriptor<T>, QueryContainer>> GetTrue<T>() where T : class { return f => new QueryContainer(); }


        public static Expression<Func<QueryContainerDescriptor<T>, QueryContainer>> And<T>(this Expression<Func<QueryContainerDescriptor<T>, QueryContainer>> first, Expression<Func<QueryContainerDescriptor<T>, QueryContainer>> second) where T : class
        {
            return first.AndAlso<T>(second, Expression.AndAlso);
        }

        public static Expression<Func<QueryContainerDescriptor<T>, QueryContainer>> Or<T>(this Expression<Func<QueryContainerDescriptor<T>, QueryContainer>> first, Expression<Func<QueryContainerDescriptor<T>, QueryContainer>> second) where T : class
        {
            return first.AndAlso<T>(second, Expression.OrElse);
        }

        private static Expression<Func<QueryContainerDescriptor<T>, QueryContainer>> AndAlso<T>(this Expression<Func<QueryContainerDescriptor<T>, QueryContainer>> expr1, Expression<Func<QueryContainerDescriptor<T>, QueryContainer>> expr2, Func<Expression, Expression, BinaryExpression> func) where T : class
        {
            var parameter = Expression.Parameter(typeof(T));

            var leftVisitor = new ReplaceExpressionVisitor(expr1.Parameters[0], parameter);
            var left = leftVisitor.Visit(expr1.Body);

            var rightVisitor = new ReplaceExpressionVisitor(expr2.Parameters[0], parameter);
            var right = rightVisitor.Visit(expr2.Body);

            return Expression.Lambda<Func<QueryContainerDescriptor<T>, QueryContainer>>(
                func(left, right), parameter);
        }

        private class ReplaceExpressionVisitor : ExpressionVisitor
        {
            private readonly Expression _oldValue;
            private readonly Expression _newValue;

            public ReplaceExpressionVisitor(Expression oldValue, Expression newValue)
            {
                _oldValue = oldValue;
                _newValue = newValue;
            }

            public override Expression Visit(Expression node)
            {
                if (node == _oldValue)
                    return _newValue;
                return base.Visit(node);
            }
        }
    }


}
